  package com.cyt.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.cyt.entity.XingWenFengLei;
import com.cyt.entity.XingWengXiZhi;
import com.cyt.util.DBManager;
// 新闻详细内容 XingWenAnLiDaoImpl.java XingWengXiZhiTwoDaoImpl.java XingWengXiZhiDaoImpl.java
//XingWenZhuanShuDaoImpl.java  这四个实现类都用的这个实体类
public class XingWengXiZhiTwoDaoImpl {
	public List<XingWengXiZhi> getXingWengXiZhiTwo(int xWid) {// 一般看sql语句有没有条件
		List<XingWengXiZhi> list = new ArrayList<XingWengXiZhi>();
		Statement st = null;
		ResultSet rs = null;
		// 1.建立连接
		Connection conn = DBManager.getConnection();
		// 2.创建Statement执行对象(用来执行sql语句)
		try {// try 正常执行
			st = conn.createStatement();
			// 3.sql语句
			String sql = "SELECT * from xingwengxizhi where xingWengId=" + xWid;
			// 4.执行sql语句,返回结果集
			rs = st.executeQuery(sql);
			// 5. 遍历结果集
			while (rs.next()) {
				int xingWengId = rs.getInt("xingWengId");
				String xWBiaoTi = rs.getString("xWBiaoTi");
				String xWengNeiRong = rs.getString("xWengNeiRong");
				String xuHao = rs.getString("xuHao");
				String renQi = rs.getString("renQi");
				int xWLeiXingId = rs.getInt("xWLeiXingId");
				String xWXiangXi = rs.getString("xWXiangXi");

				XingWengXiZhi xwxzTwo = new XingWengXiZhi(xingWengId, xWBiaoTi, xWengNeiRong, xuHao, renQi, xWLeiXingId, xWXiangXi);// 创建用户对象
				list.add(xwxzTwo);// 把user对象存入list中
			}
			return list;// 正常情况返回集合

		} catch (SQLException e) {// catch 异常执行
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;// 异常返回空
		} finally {
			DBManager.closeAll(conn, st, rs);
		}

	}
	
	/**
	 * 查询新闻的所有信息
	 * @return
	 */
	public List<XingWengXiZhi> getXingWenSuoYou (XingWengXiZhi xw){// 一般看sql语句有没有条件
		List<XingWengXiZhi> list = new ArrayList<XingWengXiZhi>();
		Statement st = null;
		ResultSet rs = null;
		// 1.建立连接
		Connection conn = DBManager.getConnection();
		// 2.创建Statement执行对象(用来执行sql语句)
		try {// try 正常执行
			st = conn.createStatement();
			// 3.sql语句
			
			String sql = "SELECT x.*,xf.xWFengLeiName FROM xingwengxizhi x,xingwenfenglei xf WHERE 1=1";
			
			if(xw.getXingWengId()!=0) {//
				sql+= " and xingWengId="+xw.getXingWengId();//拼接sql语句
				
			} 
			
			if(xw.getxWBiaoTi()!=null && !xw.getxWBiaoTi().equals("")) {
				sql+= " and xWBiaoTi like '%"+xw.getxWBiaoTi()+"%'";
			}
			if(xw.getxWLeiXingId()!=0) {  //新闻分类id外建
				sql+= " and xWLeiXingId="+xw.getxWLeiXingId();//拼接sql语句
				
			}
			sql+=" and x.xWLeiXingId=xf.xWFeiLeiId";
			
			
			
			// 4.执行sql语句,返回结果集
			rs = st.executeQuery(sql);
			// 5. 遍历结果集
			while (rs.next()) {
				int xingWengId = rs.getInt("xingWengId");
				String xWBiaoTi = rs.getString("xWBiaoTi");
				String xWengNeiRong = rs.getString("xWengNeiRong");
				String xuHao = rs.getString("xuHao");
				String renQi = rs.getString("renQi");
				int xWLeiXingId = rs.getInt("xWLeiXingId");
				String xWXiangXi = rs.getString("xWXiangXi");
				String xWFengLeiName = rs.getString("xWFengLeiName");

				XingWengXiZhi xwxzTwo = new XingWengXiZhi(xingWengId, xWBiaoTi, xWengNeiRong, xuHao, renQi, xWLeiXingId, xWXiangXi, xWFengLeiName);// 创建用户对象
				list.add(xwxzTwo);// 把user对象存入list中
			}
			return list;// 正常情况返回集合

		} catch (SQLException e) {// catch 异常执行
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;// 异常返回空
		} finally {
			DBManager.closeAll(conn, st, rs);
		}

	}
	
	/**
	 * 分页查
	 * @param xw
	 * @param page
	 * @param size
	 * @return
	 */
	public List<XingWengXiZhi> getXingWenSuoYouPage (XingWengXiZhi xw,int page, int size){// 一般看sql语句有没有条件
		List<XingWengXiZhi> list = new ArrayList<XingWengXiZhi>();
		Statement st = null;
		ResultSet rs = null;
		// 1.建立连接
		Connection conn = DBManager.getConnection();
		// 2.创建Statement执行对象(用来执行sql语句)
		try {// try 正常执行
			st = conn.createStatement();
			// 3.sql语句
			
			String sql = "SELECT x.*,xf.xWFengLeiName FROM xingwengxizhi x,xingwenfenglei xf WHERE 1=1";
			
			if(xw.getXingWengId()!=0) {//
				sql+= " and xingWengId="+xw.getXingWengId();//拼接sql语句
				
			} 
			
			if(xw.getxWBiaoTi()!=null && !xw.getxWBiaoTi().equals("")) {
				sql+= " and xWBiaoTi like '%"+xw.getxWBiaoTi()+"%'";
			}
			if(xw.getxWLeiXingId()!=0) {  //新闻分类id外建
				sql+= " and xWLeiXingId="+xw.getxWLeiXingId();//拼接sql语句
				
			}
			sql+=" and x.xWLeiXingId=xf.xWFeiLeiId LIMIT "+(page-1)*size+","+size;
			
			
			
			// 4.执行sql语句,返回结果集
			rs = st.executeQuery(sql);
			// 5. 遍历结果集
			while (rs.next()) {
				int xingWengId = rs.getInt("xingWengId");
				String xWBiaoTi = rs.getString("xWBiaoTi");
				String xWengNeiRong = rs.getString("xWengNeiRong");
				String xuHao = rs.getString("xuHao");
				String renQi = rs.getString("renQi");
				int xWLeiXingId = rs.getInt("xWLeiXingId");
				String xWXiangXi = rs.getString("xWXiangXi");
				String xWFengLeiName = rs.getString("xWFengLeiName");

				XingWengXiZhi xwxzTwo = new XingWengXiZhi(xingWengId, xWBiaoTi, xWengNeiRong, xuHao, renQi, xWLeiXingId, xWXiangXi, xWFengLeiName);// 创建用户对象
				list.add(xwxzTwo);// 把user对象存入list中
			}
			return list;// 正常情况返回集合

		} catch (SQLException e) {// catch 异常执行
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;// 异常返回空
		} finally {
			DBManager.closeAll(conn, st, rs);
		}

	}
	/**
	 * 删除新闻 通过主键
	 * @param xinWenId 传来的新闻id
	 * @return
	 */
	public int shanChuXinWenZhu(int xinWenId) {
		return DBManager.executeUpdate("DELETE FROM xingwengxizhi where xingWengId ="+xinWenId);
	}
	
	/**
	 * 通过id主键删除
     * 批量删除
	 * @param 
	 * @return
	 */
	public int shanXinWenS(String ids) {
		String sql = "delete from xingwengxizhi where xingWengId in("+ids+")";
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	
	/**
	 * 添加新闻
	 * @param xw
	 * @return 返回受影响行数
	 * 
	 */
	public int addXingWenXX(XingWengXiZhi xw ) {
		return DBManager.executeUpdate("INSERT into xingwengxizhi VALUES(null,'"+xw.getxWBiaoTi()+"','"+xw.getxWengNeiRong()+"','"+xw.getXuHao()+"','"+xw.getRenQi()+"',"+xw.getxWLeiXingId()+",'"+xw.getxWXiangXi()+"')");
	}
	
	
	
	/**
	 * 根据id查新闻详细信息,以供修改的数据回显
	 * @param xwxqId
	 * @return
	 */
	public XingWengXiZhi chaXingWenXiangQinId(int xwxqId) {
		XingWengXiZhi xwxz=null;
		Statement st = null;
		ResultSet rs = null;
		// 1.建立连接
		Connection conn = DBManager.getConnection();
		// 2.创建Statement执行对象(用来执行sql语句)
		try {// try 正常执行
			st = conn.createStatement();
			String sql = "SELECT * FROM xingwengxizhi WHERE xingWengId="+xwxqId;
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int xingWengId = rs.getInt("xingWengId");
				String xWBiaoTi = rs.getString("xWBiaoTi");
				String xWengNeiRong = rs.getString("xWengNeiRong");
				String xuHao = rs.getString("xuHao");
				String renQi = rs.getString("renQi");
				int xWLeiXingId = rs.getInt("xWLeiXingId");
				String xWXiangXi = rs.getString("xWXiangXi");

			xwxz = new XingWengXiZhi(xingWengId, xWBiaoTi, xWengNeiRong, xuHao, renQi, xWLeiXingId, xWXiangXi);
				
			}
			return xwxz;

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}

	}
	
	/**
	 * 修改新闻详细  根据id
	 * @param xwxz
	 * @return
	 */
	public int xiuGaiXinWen(XingWengXiZhi xwxz ) {
		String sql="UPDATE xingwengxizhi set xWBiaoTi='"+xwxz.getxWBiaoTi()+"',xWengNeiRong='"+xwxz.getxWengNeiRong()+"',xuHao='"+xwxz.getXuHao()+"',renQi='"+xwxz.getRenQi()+"',xWLeiXingId="+xwxz.getxWLeiXingId()+",xWXiangXi='"+xwxz.getxWXiangXi()+"' WHERE xingWengId="+xwxz.getXingWengId();
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	
}
